/**
 * Finds if an hyperlink leads to YouTube. If so
 * it adds clickable text to play it
 */
function processYouTube(/*Element*/ link, /*Element*/ message)
{
    var url = link.getAttribute("href");
    
    if(leadsToYouTube(url))
    {
        var span = document.createElement("div");
        var blank1 = document.createTextNode(" ");
        var blank2 = document.createTextNode(" ");

        span.innerHTML = "<br /><br />";
        span.message = message;
        span.url = url;

        insertAfter(blank1, link);
        insertAfter(span, blank1);
        insertAfter(blank2, span);

        appendYouTubeVideo(span, span);

    }

}


/**
 * Function returns true if given string is an url leading
 * to a YouTube webpage that contains a video. Otherwise 
 * returns false. 
 */
function leadsToYouTube(/*String*/ url)
{
    var pattern = "^http://[a-z]*.youtube.com/watch\\?.*?v=([^&]*)&?.*$";
    var reg = new RegExp(pattern);
    if(reg.test(url))
    {
        return true;
    }
    
    return false;
}


/**
 * This function appends youtube video as a child of container (first argument
 * of this function). Second argument is the viewElement, which is required
 * because of the information it carries. Returns true on success and false 
 * otherwise.
 */
function appendYouTubeVideo(/*Element*/ container, /*Element*/ viewElement)
{
    // ziskani identifikatoru videa pomoci regulerniho vyrazu
    var pattern = "^http://[a-z]*.youtube.com/watch\\?.*?v=([^&]*)&?.*$";
    var reg = new RegExp(pattern);
    var matches = reg.exec(viewElement.url);
    
    // check if the specified pattern has been found
    if (matches.lenght < 2)
    {
        return false;       
    }
    
    var videoID = matches[1];

    container.innerHTML += generateYouTubeFlashObject(videoID, viewElement.message);
    
    return true;
}

function getPlayerColor(/*String*/ className)
{
    var color;
    
    switch (className)
    {
        case "inContent":
            color = inPlayerColor;
            break;
        case "outContent":
            color = outPlayerColor;
            break;
        case "hinContent":
            color = hinPlayerColor;
            break;
        case "houtContent":
            color = houtPlayerColor;
            break;
    }
    
    return color;
}

function generateYouTubeFlashObject(/*String*/ id, /*Element*/ message)
 {
    // Calculation of the video size
    var content = message.parentNode;
    // If there is no avatar, the content.cPadding is not set.
    var cPadding = (content.cPadding) ? content.cPadding : 0;
    var width = content.clientWidth - cPadding;
    if (width < 1){
        width = content.offsetWidth - cPadding;
    }
    
    if (width < 1)
    {
        return '<span style="margin:20px auto;font-weight:bold;">error</span>';
    }
    
    width = width > 425 ? 425 : width;
    var height = width * 0.809411764;
    height = Math.round(height);
    
    var color = getPlayerColor(message.parentNode.className);
    
    // url of the video
    var videoLink = "http://www.youtube.com/v/" + id.toString();
    videoLink += "&hl=en&fs=1&color1=0x" + color + "&color2=0x" + color;    
    
    // Object tag
    var flash = '<object '
    flash += 'width="' + width + '" ';
    flash += 'height=' + height + '" >';
    
    // First param tag
    flash += "\n";
    flash += '<param ';
    flash += 'name="movie" ';    
    flash += 'value="' + videoLink + '" ';
    flash += "/>\n";
    
    // Second param tag
    flash += '<param ';
    flash += 'name="allowFullScreen" ';    
    flash += 'value="true" ';
    flash += "/>\n";
        
    // Embed tag
    flash += '<embed ';
    flash += 'src="' + videoLink + '" ';
    flash += 'type="application/x-shockwave-flash" ';
    flash += 'allowfullscreen="true" ';
    flash += 'width="' + width + '" ';
    flash += 'height="' + height + '" ';
    flash += "/>\n";

    flash += '</object>'    
    
    return flash;
 }


